# -*- coding: utf-8 -*-
import re
from slaver.items import CoursesItem
from scrapy_redis.spiders import RedisSpider


class CoursesSpider(RedisSpider):
    name = 'courses'
    #allowed_domains = ['edu.csdn.net']
    #start_urls = ['http://edu.csdn.net/']
    redis_key = 'cspider:start_urls'

    def __init__(self, *args, **kwargs):
        # Dynamically define the allowed domains list.
        domain = kwargs.pop('domain', '')
        self.allowed_domains = filter(None, domain.split(','))
        super(CoursesSpider, self).__init__(*args, **kwargs)
    
    def parse(self, response):
        res = response.xpath('//div[contains(@class,"main")]')
        item = CoursesItem()
        item['title'] = res.xpath('.//div[contains(@class,"qrcode_box_top")]/h1/text()').extract_first()
        item['lecturer'] = res.css(".professor_name a::text").extract_first()
        item['students'] = res.re("<span>([0-9]+)人已学习</span>")[0]
        item['price'] = res.re('<sapn class="money">(.*?)</sapn>')[0]
        text=res.xpath('.//div[contains(@class,"J_outline_discribe_box")]').extract()
        #print(text)
        item['gain'] = re.sub('<br>|\s+','',''.join(re.findall('<span class="tit_span">【课程收益】</span>(.*?)</div>',text[1],re.S)))
        item['discribe'] = re.sub('<br>|\s+','',''.join(re.findall('<span class="tit_span">【课程介绍】</span>(.*?)</div>',text[0],re.S)))
        yield item
